'use strict';
module.exports = (app) => {
  const { STRING, INTEGER, TEXT } = app.Sequelize;
  const Blog = app.model.define('b_blog', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    userId: {
      comment: '作者ID',
      type: INTEGER,
      allowNull: false,
    },
    userName: {
      comment: '作者名',
      type: TEXT('long'),
    },
    userAvatar: {
      comment: '作者头像',
      type: TEXT('long'),
    },
    projectId: {
      comment: '所属项目id',
      type: INTEGER,
      allowNull: false,
    },
    title: {
      comment: '文章标题',
      type: TEXT('long'),
      allowNull: false,
    },
    cover: {
      comment: '文章封面',
      type: TEXT('long'),
    },
    content: {
      comment: '文章内容',
      type: TEXT('long'),
      allowNull: false,
    },
    read_count: {
      comment: '浏览量',
      type: INTEGER,
      defaultValue: 0,
      validate: { min: 0 },
    },
    like_count: {
      comment: '点赞量',
      type: INTEGER,
      defaultValue: 0,
      allowNull: false,
      validate: { min: 0 },
    },
    comment_count: {
      comment: '评论量',
      type: INTEGER,
      defaultValue: 0,
      allowNull: false,
      validate: { min: 0 },
    },
  });
  Blog.associate = function () {
    //关联博客分类表
    app.model.Tables.Blog.Blog.hasMany(app.model.Tables.Blog.BlogClassify, {
      foreignKey: 'blogId',
      targetKey: 'id',
    });
    //关联点赞博客表
    app.model.Tables.Blog.Blog.hasMany(app.model.Tables.Blog.LikeBlog, {
      foreignKey: 'blogId',
      targetKey: 'id',
    });
  };
  return Blog;
};
